package com.chexiehui.jc.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.chexiehui.jc.R;
import com.chexiehui.jc.entity.PriceConditionInfo;
import com.chexiehui.jc.model.SortModel;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CustomChooseSortPopup extends PartShadowPopupView {

    @BindView(R.id.nsgv_sort_list)
    NoScrollListView nsgvSortList;
    private Unbinder mUnBinder;
    private int chooseIndex;
    private List<SortModel> sortData;
    private CommonAdapter<SortModel> sortAdapter;
    private ICustomChooseSortPopupListener listener;

    public CustomChooseSortPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_custom_choose_sort_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
        sortData = new ArrayList<>();
        sortData.add(new SortModel("推荐排序"));
        sortData.add(new SortModel("低价优先"));
        sortData.add(new SortModel("高价优先"));
        nsgvSortList.setAdapter(sortAdapter = new CommonAdapter<SortModel>(getContext(), R.layout.item_choose_check, sortData) {
            @Override
            protected void convert(ViewHolder viewHolder, SortModel item, int position) {
                viewHolder.setText(R.id.tv_item_title, item.getName());
                viewHolder.setTextColor(R.id.tv_item_title, ContextCompat.getColor(getContext(), chooseIndex == position ? R.color.app_color : R.color.c_333333));
                viewHolder.setVisible(R.id.iv_item_check, chooseIndex == position);
            }
        });
        nsgvSortList.setOnItemClickListener((parent, view, position, id) -> {
            chooseIndex = position;
            sortAdapter.notifyDataSetChanged();
            dismiss();
            if (listener != null) {
                listener.onChooseSort(position, sortData.get(position).getName());
            }
        });
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    public void setListener(ICustomChooseSortPopupListener listener) {
        this.listener = listener;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    public interface ICustomChooseSortPopupListener {
        void onChooseSort(int position, String name);
    }

}
